package com.contact.util.util.collections;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class LinkedMapsMap<K, T, V> extends LinkedHashMap<K, Map<T, V>> implements MapsMap<K, T, V> {
    private static final long serialVersionUID = 8358210277110077411L;

    @Override
    public List<V> getAllMappedValues() {

        List<V> list = Utils.newArrayList();

        for (Entry<K, Map<T, V>> entry : entrySet()) {
            list.addAll(entry.getValue().values());
        }
        return list;
    }

    @Override
    public V get(final K key, final T key2) {
        if (!containsKey(key)) {
            return null;
        }

        Map<T, V> map = get(key);
        if (Utils.isEmpty(map)) {
            return null;
        }

        return map.get(key2);
    }

    @Override
    public V put(final K key, final T key2, final V value) {

        Map<T, V> map = get(key);
        if (map == null) {
            map = new LinkedHashMap<T, V>();
            put(key, map);
        }

        V oldValue = map.get(key2);

        map.put(key2, value);

        return oldValue;

    }

    @Override
    public boolean containsKey(K key, T key2) {
        if (!containsKey(key)) {
            return false;
        }

        return get(key).containsKey(key2);
    }

}
